import openpyxl
class Handlexcel:
    #引入变量，这里避免下面多次写名字
    def __init__(self,filename,sheetname):
        self.filename=filename
        self.sheetname=sheetname
    """数据的读取"""
    def read_data(self):
        #直接调用对象属性self。名
        workbook = openpyxl.load_workbook(self.filename)
        sh = workbook[self.sheetname]
        res = list(sh.rows)
        title = [i.value for i in res[0]]
        # 获取第一行之外的其他行
        cases = []
        for item in res[1:]:
            data = [i.value for i in item]
            dic = dict(zip(title, data))
            cases.append(dic)
        return cases
    """数据的写入"""
    def write_data(self, row, conlumn, value):
        """

        :param row: 写入的行
        :param conlumn: 写入的列
        :param value: 写入的值
        :return:
        """
        #加载数据
        workbook = openpyxl.load_workbook(self.filename)
        sh = workbook[self.sheetname]
        #写入数据，并保存
        sh.cell(row=row, column=conlumn, value=value)
        # 2.保存到文件中-----这里文件不能是打开状态
        workbook.save(self.filename)

